package com.fsnbigdata.domain;

import com.fsnbigdata.util.ChineseInital;
import io.springlets.format.EntityFormat;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.annotations.Index;
import org.hibernate.validator.constraints.Length;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.roo.addon.javabean.annotations.RooEquals;
import org.springframework.roo.addon.javabean.annotations.RooJavaBean;
import org.springframework.roo.addon.javabean.annotations.RooToString;
import org.springframework.roo.addon.jpa.annotations.entity.JpaRelationType;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaEntity;
import org.springframework.roo.addon.jpa.annotations.entity.RooJpaRelation;
import org.springframework.util.Assert;

/**
 * = PlantConservation
 *
 * TODO Auto-generated class documentation
 *
 */
@RooJavaBean
@RooToString
@RooJpaEntity(entityFormatExpression = "#{preserveLocation}")
@RooEquals(isJpaEntity = true)
@Entity
@EntityFormat("#{preserveLocation}")
public class PlantConservation {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    @Version
    private Integer version;

    /**
     * 物种名称
     */
    @NotNull
    @Length(min = 1, max = 255,message ="植物保育中文名长度必须在1-255之间")
    private String name;

    /**
     * 物种拉丁名称
     */
    @Length(min = 0, max = 255,message ="植物保育拉丁名称长度必须在0-255之间")
    private String latin;

    /**
     * 拼音首字母
     */
    @Index(name="PlantConservation_initial")
    private String initial;

    /**
     * 生物id
     */
    @NumberFormat
    private Long organismId;

    /**
     * 科名
     */
    @Length(min = 0, max = 255,message ="植物保育科名长度必须在0-255之间")
    private String family;

    /**
     * 属名
     */
    @Length(min = 0, max = 255,message ="植物保育属名长度必须在0-255之间")
    private String genus;

    /**
     * 种名
     */
    @Length(min = 0, max = 255,message ="植物保育种名长度必须在0-255之间")
    private String species;

    /**
     * 创建者
     */
    @Length(min = 0, max = 255,message ="植物保育创建者长度必须在0-255之间")
    private String creator;

    /**
     * 引种登记
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "plantConservation", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    private Set<Introduction> introduction = new HashSet<Introduction>();

    /**
     * 植物定植
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "plantConservation", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    private Set<Planting> planting = new HashSet<Planting>();

    /**
     * 物候记录
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "plantConservation", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    private Set<PhenologyRecord> phenologyRecords = new HashSet<PhenologyRecord>();

    /**
     * 繁殖栽培
     */
    @OneToMany(cascade = { javax.persistence.CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "plantConservation", orphanRemoval = true)
    @RooJpaRelation(type = JpaRelationType.COMPOSITION)
    @OrderBy("id")
    private Set<PropagationCultivation> propagationCultivation = new HashSet<PropagationCultivation>();

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    public static final String ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE = "The given Iterable of items to add can't be null!";

    /**
     * This `equals` implementation is specific for JPA entities and uses
     * the entity identifier for it, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @param obj
     * @return Boolean
     */
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        // instanceof is false if the instance is null
        if (!(obj instanceof PlantConservation)) {
            return false;
        }
        return getId() != null && Objects.equals(getId(), ((PlantConservation) obj).getId());
    }

    /**
     * This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able
     * to identify the entity in collections after a new id is assigned to the entity, following the article in
     * https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
     *
     * @return Integer
     */
    public int hashCode() {
        return 31;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getId() {
        return this.id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Integer
     */
    public Integer getVersion() {
        return this.version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param version
     */
    public void setVersion(Integer version) {
        this.version = version;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getName() {
        return this.name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getLatin() {
        return this.latin;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param latin
     */
    public void setLatin(String latin) {
        this.latin = latin;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getInitial() {
        return this.initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param initial
     */
    public void setInitial(String initial) {
        this.initial = initial;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public Long getOrganismId() {
        return this.organismId;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param organismId
     */
    public void setOrganismId(Long organismId) {
        this.organismId = organismId;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getFamily() {
        return this.family;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param family
     */
    public void setFamily(String family) {
        this.family = family;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getGenus() {
        return this.genus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param genus
     */
    public void setGenus(String genus) {
        this.genus = genus;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getSpecies() {
        return this.species;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param species
     */
    public void setSpecies(String species) {
        this.species = species;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String getCreator() {
        return this.creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param creator
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Introduction> getIntroduction() {
        return this.introduction;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param introduction
     */
    public void setIntroduction(Set<Introduction> introduction) {
        this.introduction = introduction;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<Planting> getPlanting() {
        return this.planting;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param planting
     */
    public void setPlanting(Set<Planting> planting) {
        this.planting = planting;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<PhenologyRecord> getPhenologyRecords() {
        return this.phenologyRecords;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param phenologyRecords
     */
    public void setPhenologyRecords(Set<PhenologyRecord> phenologyRecords) {
        this.phenologyRecords = phenologyRecords;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Set
     */
    public Set<PropagationCultivation> getPropagationCultivation() {
        return this.propagationCultivation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param propagationCultivation
     */
    public void setPropagationCultivation(Set<PropagationCultivation> propagationCultivation) {
        this.propagationCultivation = propagationCultivation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return String
     */
    public String toString() {
        return "PlantConservation {" + "id='" + id + '\'' + ", version='" + version + '\'' + ", name='" + name + '\'' + ", latin='" + latin + '\'' + ", initial='" + initial + '\'' + ", organismId='" + organismId + '\'' + ", family='" + family + '\'' + ", genus='" + genus + '\'' + ", species='" + species + '\'' + ", creator='" + creator + '\'' + ", ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE + '\'' + ", ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE='" + ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE + '\'' + "}" + super.toString();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param introductionToAdd
     */
    public void addToIntroduction(Iterable<Introduction> introductionToAdd) {
        Assert.notNull(introductionToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Introduction item : introductionToAdd) {
            this.introduction.add(item);
            item.setPlantConservation(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param introductionToRemove
     */
    public void removeFromIntroduction(Iterable<Introduction> introductionToRemove) {
        Assert.notNull(introductionToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Introduction item : introductionToRemove) {
            this.introduction.remove(item);
            item.setPlantConservation(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param plantingToAdd
     */
    public void addToPlanting(Iterable<Planting> plantingToAdd) {
        Assert.notNull(plantingToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (Planting item : plantingToAdd) {
            this.planting.add(item);
            item.setPlantConservation(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param plantingToRemove
     */
    public void removeFromPlanting(Iterable<Planting> plantingToRemove) {
        Assert.notNull(plantingToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (Planting item : plantingToRemove) {
            this.planting.remove(item);
            item.setPlantConservation(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param phenologyRecordsToAdd
     */
    public void addToPhenologyRecords(Iterable<PhenologyRecord> phenologyRecordsToAdd) {
        Assert.notNull(phenologyRecordsToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (PhenologyRecord item : phenologyRecordsToAdd) {
            this.phenologyRecords.add(item);
            item.setPlantConservation(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param phenologyRecordsToRemove
     */
    public void removeFromPhenologyRecords(Iterable<PhenologyRecord> phenologyRecordsToRemove) {
        Assert.notNull(phenologyRecordsToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (PhenologyRecord item : phenologyRecordsToRemove) {
            this.phenologyRecords.remove(item);
            item.setPlantConservation(null);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param propagationCultivationToAdd
     */
    public void addToPropagationCultivation(Iterable<PropagationCultivation> propagationCultivationToAdd) {
        Assert.notNull(propagationCultivationToAdd, ITERABLE_TO_ADD_CANT_BE_NULL_MESSAGE);
        for (PropagationCultivation item : propagationCultivationToAdd) {
            this.propagationCultivation.add(item);
            item.setPlantConservation(this);
        }
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param propagationCultivationToRemove
     */
    public void removeFromPropagationCultivation(Iterable<PropagationCultivation> propagationCultivationToRemove) {
        Assert.notNull(propagationCultivationToRemove, ITERABLE_TO_REMOVE_CANT_BE_NULL_MESSAGE);
        for (PropagationCultivation item : propagationCultivationToRemove) {
            this.propagationCultivation.remove(item);
            item.setPlantConservation(null);
        }
    }

    @PrePersist
    public void prePersist(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }

    @PreUpdate
    public void preUpdate(){
        this.setInitial(ChineseInital.getFirstLetter(this.getName()));
    }
}
